import { pageTo, toast } from 'nc-lightapp-front';
import config from '../config';
import api from '../../../common/api';

// props, moduleId(区域id), key(操作的键), value（当前值）, changedrows（新旧值集合）, index（当前index）, record（行数据）,type(表格内为line，弹窗为modal), method(有blur有change)
export default function (props, moduleId, key, value, changedrows) {
    console.log('=====OnTableValueChange:', props, moduleId, key, value, changedrows);
    let paramdata = {};
    paramdata = {
        value: value,
        tableid: moduleId,
        field: key,
        uistate: pageTo.getUrlParam('status'),
        tradetype: pageTo.getUrlParam('tradetype'),
        oldvalue: '',
        pagecode: config.pagecode,
        appcode: config.appcode
    };
    paramdata.changeRowId = changedrows[0].rowid;

    if (changedrows[0].newvalue.value == changedrows[0].oldvalue.value) {
        return;
    }
    let billdata = props.createHeadAfterEventData(config.pagecode, config.headcode, [config.bodycode], moduleId, key, value);
    for (let bodyArea in billdata.card.bodys) {
        //只发送未删除的行
        billdata.card.bodys[bodyArea].rows = props.cardTable.getVisibleRows(bodyArea);
    }
    console.log('表头字段change后表头标题数据结构：', billdata);
    if (value.value == undefined || value.value == null) {
        paramdata.value = changedrows[0].newvalue.value;
    }

    let send_headdata = { billdata, paramdata };
    send_headdata.templateid = props.meta.getMeta().pageid;
    api.valueChange(send_headdata).then((data) => {
        //设置表头数据
        props.form.setAllFormValue({ [config.headcode]: data.head[config.headcode] });
        //设置表体数据
        props.cardTable.setTableData(config.bodycode, data.bodys[config.bodycode]);
    });
}
